﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.AdvertManager
{
    public partial class AdPositionManager : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(AdvertOrderUPager);


            //List<Member> list = new List<Member();
            AdPositionHelper aphelper = new AdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "Type", 1);

            AdvertOrderUPager.ItemCount = aphelper.QueryAdPositionCountBySearch(c);

            // 以下的值从BasePage中得到
            AdvertOrderUPager.UrlFormat = UrlFormat;
            AdvertOrderUPager.PrefixText = GetPreFixText(AdvertOrderUPager);

            if (mark)
            {
                PagerInit(AdvertOrderUPager);
            }

            string[] fields = new string[] { "ID", "AdPositionName", "AdScript", "Remark", "LinkURL", "Price", "CreateDate" };

            DataTable dt = new DataTable();
            dt = aphelper.QueryAdPositionBySearchToTable(c, new Order[] { new Order("Orders", OrderMode.Asc) }, AdvertOrderUPager.Begin - 1, AdvertOrderUPager.Count, fields);


            rptAdvertOrder.DataSource = dt;
            rptAdvertOrder.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptAdvertOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            TextBox txtAdPositionName = (TextBox)e.Item.FindControl("txtAdPositionName");
            TextBox txtRemark = (TextBox)e.Item.FindControl("txtRemark");
            TextBox txtLinkURL = (TextBox)e.Item.FindControl("txtLinkURL");
            TextBox txtPrice = (TextBox)e.Item.FindControl("txtPrice");
            TextBox txtAdScript = (TextBox)e.Item.FindControl("txtAdScript");

            string id = hidID.Value;

            if (e.CommandName == "edit")//编辑
            {
                string adpositionname = txtAdPositionName.Text.Trim();
                string remark = txtRemark.Text.Trim();
                string linkurl = txtLinkURL.Text.Trim();
                string price = txtPrice.Text.Trim();
                string adscript = txtAdScript.Text.Trim();

                if (string.IsNullOrEmpty(price))
                {
                    Tools.Alert("请输入价格！");
                    return;
                }
                int price1 = 0;
                if (!Int32.TryParse(price, out price1))
                {
                    Tools.Alert("价格必须为整数！");
                    return;
                }
                AdPosition apinfo=new AdPosition();
                AdPositionHelper aphelper=new AdPositionHelper();

                apinfo = aphelper.GetAdPosition(id);
                apinfo.AdPositionName = adpositionname;
                apinfo.Remark = remark;
                apinfo.LinkURL = linkurl;
                apinfo.Price = price1;
                apinfo.AdScript = adscript;

                aphelper.EditAdPosition(apinfo);

                Tools.Alert("修改成功！");
            }

            LoadContent(false);
        }
        #endregion
    }
}